﻿using System;
using System.Windows.Forms;

namespace CC.Yacht.Windows.Forms
{
    public partial class RegisterForm : Form
    {
        #region Constructor
        public RegisterForm()
        {
            InitializeComponent();
            // ReSharper disable DoNotCallOverridableMethodsInConstructor
            Text = Application.ProductName + " - Register";
            // ReSharper restore DoNotCallOverridableMethodsInConstructor
        }
        #endregion

        #region Public Events
        public event EventHandler<RegisterEventArgs> Registering;
        #endregion

        #region Public Properties
        public string Email
        {
            get { return _TextBoxEmail.Text; }
            set { _TextBoxEmail.Text = value; }
        }

        public string Password
        {
            get { return (_TextBoxPassword1.Text == _TextBoxPassword2.Text) ? _TextBoxPassword1.Text : string.Empty; }
            set
            {
                _TextBoxPassword1.Text = value;
                _TextBoxPassword2.Text = value;
            }
        }

        public string Username
        {
            get { return _TextBoxUsername.Text; }
            set { _TextBoxUsername.Text = value; }            
        }
        #endregion

        #region Private Event Handlers
        private void _ButtonCancel_Click(object sender, EventArgs e)
        {
            Close(DialogResult.Cancel);
        }

        private void _ButtonRegister_Click(object sender, EventArgs e)
        {
            if (Registering == null)
            {
                throw new InvalidOperationException("Cannot register when the Registering event is not attached.");
            }

            _ButtonRegister.Enabled = false;

            Enabled = false;
            UseWaitCursor = true;
            
            Application.DoEvents();
            OnRegistering(new RegisterEventArgs(Email, Username, Password));
        }

        private void _TextBox_TextChanged(object sender, EventArgs e)
        {
            UpdateButtonRegisterEnabled();
        }
        #endregion

        #region Protected Methods
        protected void OnRegistering(RegisterEventArgs eventArgs)
        {
            if (Registering != null)
            {
                Registering(this, eventArgs);
            }
        }
        #endregion

        #region Private Methods
        private void UpdateButtonRegisterEnabled()
        {
            _ButtonRegister.Enabled = (_TextBoxEmail.TextLength > 0 && _TextBoxPassword1.TextLength > 0 && _TextBoxUsername.TextLength > 0 && (_TextBoxPassword1.Text == _TextBoxPassword2.Text));
        }
        #endregion

        #region Public Methods
        public void Close(DialogResult dialogResult)
        {
            DialogResult = dialogResult;
            Close();
        }

        public void EndRegister(bool registeredSuccessfully)
        {
            if (registeredSuccessfully)
            {
                DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                Enabled = true;
                UseWaitCursor = false;
                UpdateButtonRegisterEnabled();
                Application.DoEvents();
            }
        }

        public void Reset()
        {
            Email = string.Empty;
            Password = string.Empty;
            Username = string.Empty;
        }
        #endregion
    }
}
